//
//  AttendanceContentViewController.swift
//  BellManage
//
//  Created by Bestsch on 16/4/13.
//  Copyright © 2016年 wsl. All rights reserved.
//

import UIKit

class AttendanceContentViewController: BaseViewController,UITableViewDelegate,UITableViewDataSource {

    var tableView: UITableView!
    
    var classid: String!
    var attArray = NSMutableArray()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        self.title = "今日考勤"
        
        InitView()
    }
    
    func InitView()  {
        
        tableView = UITableView.init(frame: CGRect(x: 0, y: 0, width: screenW, height: screenH))
        tableView.backgroundColor = UIColor.init(hexString: "f7f7f7", alpha: 1)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = UITableViewCellSeparatorStyle.singleLine
        self.view.addSubview(tableView)
        setUpRefresh()
    }
    
    func setUpRefresh() {
        
        tableView.header = MJRefreshNormalHeader.init(refreshingTarget: self, refreshingAction: #selector(AttendanceContentViewController.refresh))
        tableView.header.beginRefreshing()
    }

    func refresh() {
        /**
         "<?xml version=\"1.0\" encoding=\"utf-8\"?><rss version=\"2.0\"><channel><t>10</t><sch>%@</sch><classid>%@</classid><type>%@</type><usertype>%@</usertype><stuid></stuid></channel></rss>"
         schwebappapi.ashx
         */
        let user = getUserPojo()
        let request = requestHeader + "10</t><sch>" + user.schid + "</sch><classid>" + classid + "</classid><type>0</type><usertype>T</usertype><stuid></stuid></channel></rss>"
        let url = server + schappapi
        var result = HttpUtils.httpPostUTF(url, with: request)
        result = result?.replacingOccurrences(of: "null", with: "\"\"")
        print(result)
        
        let array = JSON(data: (result?.data(using: String.Encoding.utf8))!)["post"]
        
        if array.count != 0 {
            for i in 0..<array.count {
                let dic1 = array[i]
                let list = dic1["List"]
                attArray.addObjects(from: list.arrayValue)
            }
        }
        tableView.reloadData()
        tableView.header.endRefreshing()
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return attArray.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cellIdentifier = "AttendanceContentTableViewCellIdentifier";
        
        let cell: AttendanceContentTableViewCell = AttendanceContentTableViewCell.init(style: UITableViewCellStyle.default, reuseIdentifier: cellIdentifier)
//        cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
        cell.selectionStyle = UITableViewCellSelectionStyle.none;
        /**
         "stuserid": 111410,
         "stuname": "刘怡佳",
         "schserid": 100100,
         "classid": "SA101",
         "stuphoto": "../user/upload/ideimg/s20150101.png",
         "islate": "",
         "latetime": "",
         "isleaveearly": "",
         "leaveearlytime": "",
         "isleave": "",
         "leavetype": "",
         "leavename": "",
         "leaveremarks": "",
         "truancy": "",
         "truancytime": "",
         "remarks": ""
         */
        let dic = attArray[indexPath.row] as! JSON
        let stuname = dic["stuname"].stringValue
        var stuPhoto = dic["stuphoto"].stringValue
        let islate = dic["islate"].stringValue
        let isearlay = dic["isleaveearly"].stringValue
        let isleave = dic["isleave"].stringValue
        let leaveType = dic["leavetype"].stringValue
        let truancy = dic["truancy"].stringValue
        
        stuPhoto = stuPhoto.replacingOccurrences(of: "../..", with: "").replacingOccurrences(of: "../", with: "/view/")
        cell.userTX.sd_setImage(with: URL.init(string: server + stuPhoto), placeholderImage: UIImage.init(named: "touxiangm"))
        cell.userName.text = stuname
        if islate == "1" {
            cell.icon.image = UIImage.init(named: "kaoqin4")
        }
        if isearlay == "1"{
            cell.icon.image = UIImage.init(named: "kaoqin4")
        }
        if isleave == "1"{
            if leaveType == "1" {
                cell.icon.image = UIImage.init(named: "kaoqin2")
            }else{
                cell.icon.image = UIImage.init(named: "kaoqin5")
            }
        }
        if truancy == "1"{
            cell.icon.image = UIImage.init(named: "kaoqin3")
        }
        
        
        return cell
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
